﻿Imports System.Drawing.Drawing2D

Public Class Form1

    'Declarations
    Dim fntUpper As New Font("Segoe UI", 16, FontStyle.Bold)
    Dim clrUpper As Color = Color.White
    Dim txtUpper As String = "Input text here..."

    'Declarations lower part
    Dim fntLower As New Font("Segoe UI", 16, FontStyle.Bold)
    Dim clrLower As Color = Color.White
    Dim txtLower As String = "Input text here..."

    'Other declarations
    Dim WithEvents origImg As New PictureBox
  
    Private Sub btn_open_Click(sender As Object, e As EventArgs) Handles btn_open.Click
        Using ofd As New OpenFileDialog With {.Filter = "Image files|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff"}
            If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                pic.Image = Image.FromFile(ofd.FileName)

                origImg.Image = Image.FromFile(ofd.FileName)
                origImg.Width = Image.FromFile(ofd.FileName).Width
                origImg.Height = Image.FromFile(ofd.FileName).Height

                pic_noImage.Visible = False

                Me.Text = String.Format("{0}x{1}", origImg.Width, origImg.Height)
            End If
        End Using
    End Sub

    Private Sub btn_changefont_upper_Click(sender As Object, e As EventArgs) Handles btn_changefont_upper.Click
        Using fntdiag As New FontDialog With {.Font = fntUpper}
            If fntdiag.ShowDialog = Windows.Forms.DialogResult.OK Then
                fntUpper = fntdiag.Font
                txt_font_upper.Text = String.Format("{0}, {1}", fntdiag.Font.Name, fntdiag.Font.Size)
                pic.Invalidate()
                origImg.Invalidate()
            End If
        End Using
    End Sub

    Private Sub btn_changecolor_upper_Click(sender As Object, e As EventArgs) Handles btn_changecolor_upper.Click
        Using clrdiag As New ColorDialog With {.Color = pnl_clr_upper.BackColor}
            If clrdiag.ShowDialog = Windows.Forms.DialogResult.OK Then
                clrUpper = clrdiag.Color
                pnl_clr_upper.BackColor = clrdiag.Color
                pic.Invalidate()
                origImg.Invalidate()
            End If
        End Using
    End Sub

    Private Sub btn_changefont_lower_Click(sender As Object, e As EventArgs) Handles btn_changefont_lower.Click
        Using fntdiag As New FontDialog With {.Font = fntLower}
            If fntdiag.ShowDialog = Windows.Forms.DialogResult.OK Then
                fntLower = fntdiag.Font
                txt_font_lower.Text = String.Format("{0}, {1}", fntdiag.Font.Name, fntdiag.Font.Size)
                pic.Invalidate()
                origImg.Invalidate()
            End If
        End Using
    End Sub

    Private Sub btn_changecolor_lower_Click(sender As Object, e As EventArgs) Handles btn_changecolor_lower.Click
        Using clrdiag As New ColorDialog With {.Color = pnl_clr_lower.BackColor}
            If clrdiag.ShowDialog = Windows.Forms.DialogResult.OK Then
                clrLower = clrdiag.Color
                pnl_clr_lower.BackColor = clrdiag.Color
                pic.Invalidate()
                origImg.Invalidate()
            End If
        End Using
    End Sub

    Private Sub btn_changetext_lower_Click(sender As Object, e As EventArgs) Handles btn_changetext_lower.Click
        txtLower = txt_text_lower.Text
        pic.Invalidate()
        origImg.Invalidate()
    End Sub

    Private Sub btn_changetext_upper_Click(sender As Object, e As EventArgs) Handles btn_changetext_upper.Click
        txtUpper = txt_text_upper.Text
        pic.Invalidate()
        origImg.Invalidate()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False

        'Initialization
        txt_font_lower.Text = String.Format("{0}, {1}", fntLower.Name, fntLower.Size)
        txt_font_upper.Text = String.Format("{0}, {1}", fntUpper.Name, fntUpper.Size)

        pnl_clr_lower.BackColor = clrLower
        pnl_clr_upper.BackColor = clrUpper

        txt_text_lower.Text = txtLower
        txt_text_upper.Text = txtUpper
    End Sub

    Private Sub pic_Paint(sender As Object, e As PaintEventArgs) Handles pic.Paint
        Dim rct As Rectangle = pic.ClientRectangle

        Dim g As Graphics = e.Graphics

        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
        g.CompositingQuality = CompositingQuality.HighQuality

        If pic.Image IsNot Nothing Then
            'Dim rctImg As Rectangle = origImg.ClientRectangle

            Dim size_txt_upper As SizeF = g.MeasureString(txtUpper, fntUpper)
            Dim size_txt_lower As SizeF = g.MeasureString(txtLower, fntLower)

            Dim brush_text_upper As New SolidBrush(clrUpper)
            Dim brush_text_lower As New SolidBrush(clrLower)

            Dim rct_txt_upper As New Rectangle(rct.X + (rct.Width / 2) - (size_txt_upper.Width / 2), rct.Y + 5, size_txt_upper.Width + 10, size_txt_upper.Height)
            'Dim rct_img_txtupper As New Rectangle(rctImg.X + (rctImg.Width / 2) - (size_txt_upper.Width / 2), rctImg.Y + 5, size_txt_upper.Width, size_txt_upper.Height)

            Dim rct_txt_lower As New Rectangle(rct.X + (rct.Width / 2) - (size_txt_lower.Width / 2), (rct.Y + rct.Height) - (size_txt_lower.Height + 5), size_txt_lower.Width + 10, size_txt_lower.Height)


            'Dim grpathUpper As New GraphicsPath
            'grpathUpper.AddString(txtUpper, fntUpper.FontFamily, FontStyle.Bold, 0, rct_txt_upper, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})

            g.DrawString(txtUpper, fntUpper, brush_text_upper, rct_txt_upper)
            'g.DrawString(txtUpper, fntUpper, brush_text_upper, rct_img_txtupper)
            'g.DrawPath(Pens.Black, grpathUpper)

            g.DrawString(txtLower, fntLower, brush_text_lower, rct_txt_lower)
        End If
    End Sub

    Private Sub origImg_Paint(sender As Object, e As PaintEventArgs) Handles origImg.Paint
        Dim rct As Rectangle = origImg.ClientRectangle

        Dim g As Graphics = e.Graphics

        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
        g.CompositingQuality = CompositingQuality.HighQuality

        If pic.Image IsNot Nothing Then
            Dim fnt_bigger_upper As Font
            Dim fnt_bigger_lower As Font

            If origImg.Width > 1280 AndAlso origImg.Width > 720 Then
                fnt_bigger_upper = New Font(fntUpper.FontFamily, fntUpper.Size * 2, fntUpper.Style)
                fnt_bigger_lower = New Font(fntLower.FontFamily, fntLower.Size * 2, fntLower.Style)
            Else
                fnt_bigger_upper = New Font(fntUpper.FontFamily, fntUpper.Size, fntUpper.Style)
                fnt_bigger_lower = New Font(fntLower.FontFamily, fntLower.Size, fntLower.Style)
            End If

            Dim size_txt_upper As SizeF = g.MeasureString(txtUpper, fnt_bigger_upper)
            Dim size_txt_lower As SizeF = g.MeasureString(txtLower, fnt_bigger_lower)

            Dim brush_text_upper As New SolidBrush(clrUpper)
            Dim brush_text_lower As New SolidBrush(clrLower)

            Dim rct_txt_upper As New Rectangle(rct.X + (rct.Width / 2) - (size_txt_upper.Width / 2), rct.Y + 5, size_txt_upper.Width + 10, size_txt_upper.Height)

            Dim rct_txt_lower As New Rectangle(rct.X + (rct.Width / 2) - (size_txt_lower.Width / 2), (rct.Y + rct.Height) - (size_txt_lower.Height + 5), size_txt_lower.Width + 10, size_txt_lower.Height)

            'Dim grpathUpper As New GraphicsPath
            'grpathUpper.AddString(txtUpper, fntUpper.FontFamily, FontStyle.Bold, 0, rct_txt_upper, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})

            g.DrawString(txtUpper, fnt_bigger_upper, brush_text_upper, rct_txt_upper)

            'g.DrawPath(Pens.Black, grpathUpper)

            g.DrawString(txtLower, fnt_bigger_lower, brush_text_lower, rct_txt_lower)
        End If
    End Sub

    Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
      
        Dim bmp As New Bitmap(origImg.Width, origImg.Height)
        'bmp.SetResolution(pic.Image.HorizontalResolution, pic.Image.VerticalResolution)

        Using sfd As New SaveFileDialog With {.Filter = "Image files|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff"}
            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                origImg.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))

                bmp.Save(sfd.FileName)
            End If
        End Using
    End Sub

    Private Sub txt_text_upper_KeyDown(sender As Object, e As KeyEventArgs) Handles txt_text_upper.KeyDown
        If e.KeyCode = Keys.Enter Then
            txtUpper = txt_text_upper.Text
            pic.Invalidate()
            origImg.Invalidate()
        End If
    End Sub

    Private Sub txt_text_lower_KeyDown(sender As Object, e As KeyEventArgs) Handles txt_text_lower.KeyDown
        If e.KeyCode = Keys.Enter Then
            txtLower = txt_text_lower.Text
            pic.Invalidate()
            origImg.Invalidate()
        End If
    End Sub

    Private Sub btn_info_Click(sender As Object, e As EventArgs) Handles btn_info.Click
        AboutBox1.ShowDialog()
    End Sub
End Class
